package projectx.core.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;


public final class ProjectXProperties {

    public static Properties load( String fileName )
        throws ProjectXSystemException
    {
        return ResourceUtil.loadProperties( fileName, true );
    }

    public static String getProperty( Properties prop, String fileName, String key )
        throws ProjectXSystemException
    {
        String  value = prop.getProperty( key );

        if ( value == null || value.length() == 0 ){
            throw new ProjectXSystemException( "E0101", fileName, key );
        }

        return value;
    }

    public static int getPropertyInt( Properties prop, String fileName, String key )
        throws ProjectXSystemException
    {
        String  value = getProperty( prop, fileName, key );

        try {
            return Integer.parseInt( value );
        }
        catch ( NumberFormatException e ){
            throw new ProjectXSystemException( "E0102", value, fileName, key );
        }
    }

    public static String[] getPropertyList( Properties prop, String fileName, String startKey )
        throws ProjectXSystemException
    {
        List    keyList = new ArrayList();

        for ( Enumeration e = prop.keys() ; e.hasMoreElements() ; ) {
            String key = (String)e.nextElement();
            if ( key.startsWith( startKey ) ) {
                keyList.add( key );
            }
        }

        Collections.sort( keyList );


        List    valueList = new ArrayList();

        for ( int i = 0 ; i < keyList.size() ; i++ ){
            valueList.add( getProperty( prop, fileName, (String)keyList.get( i ) ) );
        }

        return (String[])valueList.toArray( new String[ valueList.size() ] );
    }

    private static final Logger _log = Logger.getLogger( ProjectXProperties.class.getName() );

    private static boolean      _initialized;
    private static Properties   _prop;
    private static String       _fileName;


    public static void initialize( String fileName ) throws ProjectXSystemException {

        if ( !_initialized ){

            synchronized( ProjectXProperties.class ){

                if ( !_initialized ){

                    _fileName = fileName;

                    _log.info( "Initialize using [" + _fileName + "]..." );

                    _prop = load( _fileName );

                    _log.info( "Initialized successfully" );

                    _initialized = true;
                }
            }
        }
    }

    public static String getProperty( String key ) throws ProjectXSystemException {

        return getProperty( _prop, _fileName, key );
    }
    
    public static int getPropertyInt( String key ) throws ProjectXSystemException {

        return getPropertyInt( _prop, _fileName, key );
    }

    public static String getProperty( String key, String defaultValue ){

        try {
           return getProperty( _prop, _fileName, key );
        }
        catch ( ProjectXSystemException e ){
            _log.warn( e );
            _log.warn( "Use default value [" + defaultValue + "]" );

            return defaultValue;
        }
    }

    public static int getPropertyInt( String key, int defaultValue ) {

        try {
           return getPropertyInt( _prop, _fileName, key );
        }
        catch ( ProjectXSystemException e ){
            _log.warn( e );
            _log.warn( "Use default value [" + defaultValue + "]" );

            return defaultValue;
        }
    }

    public static String[] getPropertyList( String startKey )
        throws ProjectXSystemException
    {
        return getPropertyList( _prop, _fileName, startKey );
    }

    public static boolean isInitialized(){ return _initialized; }

    public static String getFilename(){ return _fileName; }

    public static Enumeration keys(){ return _prop.keys(); }

    public static Set keySet(){ return _prop.keySet(); }

    private ProjectXProperties(){}
}
